package com.hotel.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.hotel.entidade.Quarto;

public class QuartoDAO {

	private Session session;

	public QuartoDAO(Session session) {
		this.session = session;
	}
	
	public void insere(Quarto quarto) {
		session.save(quarto);
		session.beginTransaction().commit();
		session.close();
	}

	public void deleta(Quarto quarto) {
		session.delete(quarto);
		session.beginTransaction().commit();
	}

	public void atualisa(Quarto quarto) {
		session.update(quarto);
		session.beginTransaction().commit();
	}

	public Quarto buscaPorId(int id) {
		return (Quarto) session.load(Quarto.class, id);
	}

	public List<Quarto> pesquisarQuarto(String keyword){
		@SuppressWarnings("unchecked")
		List<Quarto> list = session.createCriteria(Quarto.class)
			.add(Restrictions.ilike("numero", keyword, MatchMode.ANYWHERE))
            .list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Quarto> listaTodos() {
		return session.createCriteria(Quarto.class).list();
	}

}
